package com.gougou.manager.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gougou.manager.pojo.TbContentCategory;
import com.gougou.manager.service.MPTbContentCategoryService;
import com.gougou.manager.service.impl.TbContentCategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 内容分类 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2018-09-07
 */
@RestController
@RequestMapping("/tbContentCategory")
public class TbContentCategoryController {
    @Autowired
    private MPTbContentCategoryService contentCategoryService;
    @Autowired
    private TbContentCategoryServiceImpl tbContentCategoryService;

    /**
     * 查询所有内容分类的节点
     * @param id
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResponseEntity< List<TbContentCategory>> getContentCategoryList(@RequestParam(value = "id",defaultValue ="0" )long id){
        EntityWrapper<TbContentCategory> wrapper = new EntityWrapper<TbContentCategory>();
        wrapper.where("parent_id={0}",id);
        try {
            return  ResponseEntity.ok(contentCategoryService.selectList(wrapper));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 新增内容类型
     * @param contentCategory
     * @return
     */
    @RequestMapping( value = "/saveContentVategory",method = RequestMethod.POST)
    public  ResponseEntity<TbContentCategory> saveContentVategory(TbContentCategory contentCategory){

        try {
            Date date = new Date();
            contentCategory.setCreated(date);
            contentCategory.setUpdated(date);
            contentCategory.setId(null);
            contentCategory.setIsParent(0);
            contentCategory.setSortOrder(1);
            contentCategory.setStatus(1);
            contentCategoryService.insert(contentCategory);
            TbContentCategory parent = contentCategoryService.selectById(contentCategory.getParentId());
            if (parent.getIsParent()!=1){
                parent.setIsParent(1);
                contentCategoryService.updateById(parent);
            }
            return  ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);

        }catch (Exception e){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

    /**
     * 更新内容名称通过Id
     * @param id
     * @param name
     * @return
     */
    @RequestMapping( value = "/renameContentVategory",method = RequestMethod.PUT)
    public  ResponseEntity<Void> renameContentVategoryById(Long id,String name){
        TbContentCategory contentCategory = new TbContentCategory();
        try {
            Date date = new Date();
            contentCategory.setId(id);
            contentCategory.setName(name);
            contentCategory.setUpdated(date);
            contentCategoryService.updateById(contentCategory);
            return  ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);

        }catch (Exception e){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }

    /**
     * 删除内容分类
     * @param contentCategory
     * @return
     */
    @RequestMapping( value = "/deleteContentVategory")
    public  ResponseEntity<Void> deleteContentVategory(TbContentCategory contentCategory){
        try {
            tbContentCategoryService.deleteAll(contentCategory);
            return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

    }
}

